/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fullalbum.album;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import fullalbum.bean.Figure;
import fullalbum.bean.UserAlbum;
import fullalbum.dao.FigureDAO;
import fullalbum.dao.UserAlbumDAO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leoomoreira
 */
public class PrintUserAlbumServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String albumId = request.getParameter("albumId");
        String userId = request.getParameter("userId");
        boolean ownerAlbum = false;

        HttpSession session = request.getSession();
        if (session.getAttribute("userId").toString().equals(userId)) {
            ownerAlbum = true;
        }

        Document document = new Document(PageSize.A4);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        /*
        SimpleDateFormat sfDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Font label = FontFactory.getFont("Arial", 8f);
        Font text = FontFactory.getFont("Arial", 8f);
        text.setStyle(Font.BOLD);
        Font tableTitle = FontFactory.getFont("Arial", 8f);
        tableTitle.setStyle(Font.BOLD);
        Font tableContent = FontFactory.getFont("Arial", 8f);
        */

        try {
            PdfWriter.getInstance(document, baos);
            document.open();

            UserAlbumDAO userAlbumDAO = new UserAlbumDAO();
            UserAlbum userAlbum = userAlbumDAO.getByUserIdAndAlbumId(Integer.parseInt(userId), Integer.parseInt(albumId));
            FigureDAO figureDAO = new FigureDAO();
            List<Figure> figureList = figureDAO.getFiguresByAlbum(Integer.parseInt(albumId));
            List<Figure> figureWithinAlbumList = figureDAO.getFiguresWithinAlbum(Integer.parseInt(userId), Integer.parseInt(albumId));
            List<Figure> repeatedFigureWithinAlbumList = figureDAO.getRepeatedFiguresWithinAlbum(Integer.parseInt(userId), Integer.parseInt(albumId));

            PdfPTable tempTable = new PdfPTable(1);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

            tempTable.addCell(userAlbum.getAlbum().getName() + " - " + userAlbum.getUser().getName());

            document.add(tempTable);
            document.add(new Paragraph(" "));

            tempTable = new PdfPTable(1);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            tempTable.addCell((ownerAlbum) ? "Your album" : "User album");
            document.add(tempTable);
            document.add(new Paragraph(" "));

            tempTable = new PdfPTable(20);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            int count = 0;
            List<Figure> figureTempList = new ArrayList<Figure>();
            while (figureList.size() > 0) {
                figureTempList.add(figureList.get(0));
                Figure f = figureList.remove(0);
                PdfPCell c = new PdfPCell(new Phrase(f.getId()));
                c.setHorizontalAlignment(Element.ALIGN_CENTER);
                c.setVerticalAlignment(Element.ALIGN_MIDDLE);
                if (figureWithinAlbumList.contains(f)) {
                    c.setBackgroundColor(BaseColor.GRAY);
                }
                tempTable.addCell(c);
                count++;
                if (count == 20) {
                    count = 0;
                }
            }
            while (count > 0) {
                PdfPCell c = new PdfPCell(new Phrase(" "));
                tempTable.addCell(c);
                count--;
            }

            document.add(tempTable);
            document.add(new Paragraph(" "));
            document.newPage();

            tempTable = new PdfPTable(1);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

            tempTable.addCell(userAlbum.getAlbum().getName() + " - " + userAlbum.getUser().getName());

            document.add(tempTable);
            document.add(new Paragraph(" "));

            tempTable = new PdfPTable(1);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            tempTable.addCell("Repeated figures");
            document.add(tempTable);
            document.add(new Paragraph(" "));

            tempTable = new PdfPTable(20);
            tempTable.setWidthPercentage(100f);
            tempTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            count = 0;
            while (figureTempList.size() > 0) {
                Figure f = figureTempList.remove(0);
                PdfPCell c = new PdfPCell(new Phrase(f.getId()));
                c.setHorizontalAlignment(Element.ALIGN_CENTER);
                c.setVerticalAlignment(Element.ALIGN_MIDDLE);
                if (repeatedFigureWithinAlbumList.contains(f)) {
                    c.setBackgroundColor(BaseColor.GRAY);
                }
                tempTable.addCell(c);
                count++;
                if (count == 20) {
                    count = 0;
                }
            }
            while (count > 0) {
                PdfPCell c = new PdfPCell(new Phrase(" "));
                tempTable.addCell(c);
                count--;
            }

            document.add(tempTable);
            
        } catch (DocumentException e) {
            
        } finally {
            document.close();
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            ServletOutputStream out = response.getOutputStream();
            baos.writeTo(out);
            out.flush();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
